@extends('admin.layout')
@section('content')
  <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <h1>
                职位分类管理
                <small>列表</small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="#"><i class="fa fa-dashboard"></i>主页</a></li>
                <li><a href="#">职位分类管理</a></li>
                <li class="active">列表</li>
            </ol>
        </section>

        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-xs-12">
                    <div class="box">
                        <div class="box-header">
                            <h3 class="box-title">快速查看职位分类</h3>
                        </div><!-- /.box-header -->
                        <div class="box-body">
                             @if(session('info'))
                             <div class="alert alert-danger alert-dismissable">
                                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                                <h4><i class="icon fa fa-ban"></i> 提示!</h4>
                                <p>{{ session('info') }}</p>
                            </div>
                            @endif
                            <div class='row'>

                            <div class='col-md-4'>
                           		<select form='frm' name='num' class="form-control">
                                        <option {{ empty($request['num'])?'selected':($request['num']==10?'selected':'') }}>10</option>
                                        <option {{ empty($request['num'])?'':($request['num']==25?'selected':'') }}>25</option>
                                        <option {{ empty($request['num'])?'':($request['num']==50?'selected':'') }}>50</option>
                                        <option {{ empty($request['num'])?'':($request['num']==100?'selected':'') }}>100</option>
                                </select>
                           	</div>
                           	<div class='col-md-4 col-md-offset-4'>
	                            <form id='frm' action="{{ url('/admin/cate') }}" method='get'>
		                            <div class="input-group input-group-sm">
		                                <input type="text" value="{{ $request['search'] or '' }}" name='search' class="form-control">
					                    <span class="input-group-btn">
					                      <button class="btn btn-info btn-flat">搜索</button>
					                    </span>
					            	</div>	
	                            </form>
                            </div>
                           </div>
                            <br>
                            <table id="example2" class="table table-bordered table-hover">
                                <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>职位分类名</th>
                                    <th>父类名称</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                @foreach($data as $cate)
                                <tr>
                                    <td class='ids'>{{ $cate -> id }}</td>
                                    <td class='name'>{{ $cate -> name }}</td>
                                    <td>{{ $cate -> parentName }}</td>
                                    <td> <a style='float:left;' class="btn btn-primary btn-small" href="{{ url('/admin/cate/') }}/{{ $cate -> id }}/edit">编辑</a> 
                                        <form style="float:left;margin-left: 10px;" action="{{ url('/admin/cate') }}/{{ $cate -> id }}" method='post'>
                                            {{ csrf_field() }}
                                            {{ method_field('DELETE') }}
                                            <button class="btn btn-warning">删除</button>
                                        </form>
                                    </td>
                                </tr>
                                @endforeach
                                </tbody>
                            </table>
                            {!! $data -> appends($request) -> render() !!}
                        </div><!-- /.box-body -->
                    </div><!-- /.box --> 
                </div><!-- /.col -->
            </div><!-- /.row -->
        </section><!-- /.content -->
    </div><!-- /.content-wrapper -->
    <script>
    window.onload=function(){
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        }); 
        // ajax更新职位分类状态开始
        // start
        // alert($);// 验证jQuery引入是否正确
        $('.status').on('click',function(){
            // alert('11');
            // id 参数
            // var id = $(this).parent().children().first().html();
            var id = $(this).parent().find('.ids').html();
            var td = $(this);
            // console.log(id);
            $.ajax({
                // 发送ajax的url中的 {{--  --}} 必须加上引号,否则会认为js的语法去执行
                url:'{{ url("/admin/user/ajaxUpdate") }}',
                type:"POST",
                data:{id:id},
                success:function(data)
                {
                    // alert(data);
                    if(data == '0')
                    {
                        td.html('启用');
                    }else if(data == '1')
                    {
                        td.html('禁用');
                    }else
                    {
                        alert('修改失败');
                    }
                },
                dataType:'json'
            });
        });
        // ajax更新职位分类状态结束
        // ajax End
        function dblClick()
        {
            // alert($(this).html());
            // 获取id
            var id = $(this).parent().find('.ids').html();
            // 当前对象
            var td = $(this);

            // 获取当前内容的值
            var oldName = td.html();
            // 设置input框
            var inp = $('<input type="text" />');
            // 设置value的值
            inp.val(oldName);

            // 替换当前对象的值
            $(this).html(inp);

            // 获取焦点
            inp.select();

            // 添加失去焦点事件
            inp.on('blur',function(){
                // alert('blur');
                var newName = inp.val();

                // 书写ajax
                $.ajax({
                    url:"{{ url('/admin/user/ajaxName') }}",
                    type:"POST",
                    data:{id:id,name:newName},
                    success:function(data)
                    {
                        // 添加双击事件
                        td.on('dblclick',dblClick);
                        // alert(data);
                        if(data == '0')
                        {
                            td.html(newName);
                        }else if(data == '1')
                        {
                            td.html(oldName);
                        }else
                        {
                            inp.val(oldName);
                            alert('职位分类名已存在');
                            // 将双击事件清除
                            td.unbind('dblclick');
                        }
                    },
                    dataType:'json'
                });
                
            });
            // 将双击事件清除
            $(this).unbind('dblclick');
            
        }
        // 双击职位分类名实现编辑,移出完成操作Start
        $('.name').on('dblclick',dblClick);
        // End

        // console.log(document.forms[1].elements[0]);
        // var s = document.forms[1].elements[0];
        // s.onchange=function(){
        //      for(var n in document.forms[1].elements)
        //      {
        //          console.log(document.forms[1].elements[n].value);
        //      }
        // };
    };
    </script>
@endsection